import type { FindLimitDto, FindList } from "./auth";
import type { category } from "./category";
import type { Tags } from "./tag";
import type { Users } from "./user";

export interface createPost {
  title: string;
  content?: string;
  contentHtml?: string;
  tags: number[];
  category: number | null;
  coverUrl?: string;
  status?: "publish" | "draft";
  isRecommend?: boolean;
  isCommentOpen?: boolean;
}
export interface updatePost extends createPost {
  author?: Users | null;
  summary: string;
}
export interface Posts {
  id: string;
  title: string;
  content?: string;
  contentHtml?: string;
  summary: string;
  coverUrl: string;
  count: number;
  likeCount: number;
  isRecommend: boolean;
  status: "draft" | "publish";
  author: Users;
  category: category;
  tags: Tags[];
  publishTime: Date;
  createTime: Date;
  updateTime: Date;
}
export interface PostListShow {
  id: string;
  title: string;
  coverUrl: string;
  isRecommend: boolean;
  status: string;
  author: { nickname: string; id: string };
  category: {
    id: number;
    name: string;
  };
  tags: {
    id: number;
    name: string;
  }[];
  count: number;
  likeCount: number;
}

export const create = (data: createPost) => {
  return useServerRequest<string>("/posts", {
    method: "POST",
    body: { ...data },
  });
};

export interface PostQueryFind extends FindLimitDto {
  status?: "draft" | "publish";
  title?: string;
}

export const findAll = (query?: PostQueryFind) => {
  return useServerRequest<FindList<Posts>>("/posts", {
    method: "GET",
    params: { ...query },
  });
};

export const findByIdAdmin = (id: string) => {
  return useServerRequest<Posts>(`/posts/findByIdAdmin/${id}`, {
    method: "GET",
  });
};
export const updateOne = (id: string, body: createPost) => {
  return useServerRequest(`/posts/${id}`, {
    method: "PATCH",
    body: body,
  });
};

export const removeOne = (id: string) => {
  return useServerRequest(`/posts/${id}`, {
    method: "DELETE",
  });
};
